<?php

namespace App\Http\Requests;
use Illuminate\Validation\Rule;

/**
 * 表单验证
 * Class UserSaveRequest
 * @package App\Http\Requests
 */
class UserSaveRequest extends BaseRequest
{
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $id = $this->get('id', 0);
        $rules = [
            'username' => [
                'required',
                'between:2,20',
                Rule::unique('users', 'username')->where('deleted_at', 0)->ignore($id)
            ],
            'name' => [
                'required',
                'between:2,20'
            ],
            'role_tag' => [
                'required',
            ]
        ];

        if (!$id) {
            $rules['password'] = ['required', 'between:6,20', 'confirmed'];
        }


        return $rules;
    }

    public function messages()
    {
        return [
            'username.required' => '请输入用户名',
            'role_tag.required' => '请选择岗位',
            'username.between' => '用户名字符长度:min-:max',
            'username.unique' => '用户名已存在',
            'name.required' => '请输入姓名',
            'name.between' => '姓名字符长度:min-:max',
            'password.required' => '请输入新密码',
            'password.between' => '密码字符长度:min-:max',
            'password.confirmed' => '两次密码不一致'
        ];
    }
}
